package com.zd.web.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zd.entity.User;
import com.zd.service.UserService;
import com.zd.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		UserService us = new UserServiceImpl();
		System.out.println("进入添加用户");
		//获取user数据
		String userNickName = request.getParameter("userName");
		String userPass = request.getParameter("userPass");
		String userPhone = request.getParameter("userNum");
		String userPassAgain = request.getParameter("userPassAgain");
		System.err.println(userNickName+"wwwwwww");
		//获取时间
		SimpleDateFormat formarrter = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		Date date = new Date(System.currentTimeMillis());
		
		//将它们存入user对象
		User user = new User(userNickName,userPass,userPhone,0,1,date,0,0);
		
		boolean prompt = false;
		
		//判断用户名格式
		if (userNickName == null) {
			String userNickNamePrompt = "*用户名不能为空";
			request.getSession().setAttribute("userNickNamePrompt", userNickNamePrompt);
			prompt = true;
		}else {
			request.getSession().setAttribute("userNickNamePrompt", "");
		}
		
		//判断手机号码格式
		if (userPhone.length() != 11) {
			String userPhonePrompt = "*请输入正确的手机号码";
			request.getSession().setAttribute("userPhonePrompt", userPhonePrompt);
			prompt = true;
		}else {
			request.getSession().setAttribute("userPhonePrompt", "");
		}
		
		//判断手机号码是否存在
		if(us.select(user) != null) {
			String userPhonePromptZC = "*该手机号码已被注册";
			request.getSession().setAttribute("userPhonePromptZC", userPhonePromptZC);
			prompt = true;
		}else {
			request.getSession().setAttribute("userPhonePromptZC", "");
		}
		//判断密码格式
		if (!(userPass.length()>6 && userPass.length()<20)) {
			String passPrompt = "*密码格式不正确";
			request.getSession().setAttribute("passPrompt", passPrompt);
			prompt = true;
		}else {
			request.getSession().setAttribute("passPrompt", "");
		}
		
		//判断两次密码是否相同
		if (!(userPass.equals(userPassAgain))) {
			String passPromptAgain = "*您两次输入的密码不一样";
			request.getSession().setAttribute("passPromptAgain", passPromptAgain);
			prompt = true;
		}else {
			request.getSession().setAttribute("passPromptAgain", "");
		}
		
		//判断有没有以上问题
		if (prompt) {
			request.getRequestDispatcher("rigister.jsp").forward(request, response);
			return;
		}
		
		//将user上传到Session
		request.getSession().setAttribute("user", user);
		
		//注册成功后转发到注册成功页面
		if(us.add(user)) {
			String uId = us.select(user);
			request.getSession().setAttribute("uId", uId);
			request.getRequestDispatcher("rigisterResult.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		UserService us = new UserServiceImpl();
		//获取user数据
		String userNickName = request.getParameter("userNickName");
		String userPass = request.getParameter("userPass");
		String userPhone = request.getParameter("userPhone");
		
		
		//获取时间
//		SimpleDateFormat formarrter = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		Date date = new Date(System.currentTimeMillis());
		
		//将它们存入user对象
		User user = new User(userNickName,userPass,userPhone,0,1,date,0,0);
		
		boolean prompt = false;
		
		//判断用户名格式
		if (userNickName == null) {
			String userNickNamePrompt = "*用户名不能为空";
			request.getSession().setAttribute("userNickNamePrompt", userNickNamePrompt);
			prompt = true;
		}else {
			request.getSession().setAttribute("userNickNamePrompt", "");
		}
		
		//判断手机号码格式
		if (userPhone.length() != 11) {
			String userPhonePrompt = "*请输入正确的手机号码";
			request.getSession().setAttribute("userPhonePrompt", userPhonePrompt);
			prompt = true;
		}else {
			request.getSession().setAttribute("userPhonePrompt", "");
		}
		
		//判断手机号码是否存在
		if(us.select(user) != null) {
			String userPhonePromptZC = "*该手机号码已被注册";
			request.setAttribute("userPhonePromptZC", userPhonePromptZC);
			prompt = true;
		}else {
			request.setAttribute("userPhonePromptZC", "");
		}
		//判断密码格式
		if (!(userPass.length()>6 && userPass.length()<20)) {
			String passPrompt = "*密码格式不正确";
			request.setAttribute("passPrompt", passPrompt);
			prompt = true;
		}else {
			request.setAttribute("passPrompt", "");
		}
		
		
		//判断有没有以上问题
		if (prompt) {
			request.getRequestDispatcher("userAdd.jsp").forward(request, response);
			return;
		}
		
		//注册成功后转发到注册成功页面
		if(us.add(user)) {
//			String uId = us.select(user);
			
			response.sendRedirect("user.jsp");
		}
	}

}
